﻿using MyHome.Configuration.Net;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using ViewModelBase;

namespace MyHome.Configuration.ViewModel
{
    class PluginTypesViewModel : ViewModelBase.ViewModel
    {
        private PluginHostViewModel host;
        private IDictionary<string, Type> dict;

        #region PluginTypes property

        private ObservableCollection<string> pluginTypes;

        public ObservableCollection<string> PluginTypes
        {
            get
            {
                return pluginTypes;
            }
            set
            {
                if (PluginTypes != value)
                {
                    pluginTypes = value;
                    OnPropertyChanged("PluginTypes");
                }
            }
        }

        #endregion PluginTypes property

        #region SelectedPluginType property

        private string selectedPluginType;

        public string SelectedPluginType
        {
            get
            {
                return selectedPluginType;
            }
            set
            {
                if (SelectedPluginType != value)
                {
                    selectedPluginType = value;
                    OnPropertyChanged("SelectedPluginType");
                }
            }
        }

        #endregion SelectedPluginType property

        #region AddCommand

        private ICommand addCommand;

        public ICommand AddCommand
        {
            get
            {
                if (addCommand == null)
                    addCommand = new RelayCommand(async param => await ExecuteAddCommand(param));

                return addCommand;
            }
        }

        private async Task ExecuteAddCommand(object param)
        {
            var template = new PluginTemplate { Type = SelectedPluginType };
            var updated = template;// await host.Model.AddPluginAsync(template);
            host.Plugins.Add(new PluginViewModel(updated, host));
        }

        #endregion AddCommand

        #region GetPluginTypesCommand

        private ICommand getPluginTypesCommand;

        public ICommand GetPluginTypesCommand
        {
            get
            {
                if (getPluginTypesCommand == null)
                    getPluginTypesCommand = new RelayCommand(async param => await ExecuteGetPluginTypesCommand(param));

                return getPluginTypesCommand;
            }
        }

        private async Task ExecuteGetPluginTypesCommand(object param)
        {
        //    dict = await host.Model.GetPluginTypesAsync();
        //    PluginTypes = new ObservableCollection<string>(dict.Select(pair => pair.Key));
        }

        #endregion GetPluginTypesCommand
    

        public PluginTypesViewModel(PluginHostViewModel host)
        {
            this.host = host;
        }
    
    }
}
